//
//  BCProjectDetailRichListCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/26.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCProjectDetailRichListCell.h"
//
#import "BCProjectDetailRichCollectionCell.h"

#import "BCProjectDetailResponse.h"

static CGFloat BCProjectDetailRichListCellTopHeight = 90.0;

@interface BCProjectDetailRichListCell () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UILabel *richLabel;

/**
 *richCollection
 */
@property (nonatomic, strong) UICollectionView *richCollection;


/**
 *richSource
 */
@property (nonatomic, copy) NSArray *richSource;

@end

@implementation BCProjectDetailRichListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.richLabel = [UILabel new];
    self.richLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.richLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
    self.richLabel.numberOfLines = 0;
    self.richLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.richLabel];
    [self.richLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).offset(2*BCProjectDetailCellLeftPadding);
        make.bottom.mas_equalTo(self.contentView.mas_top).offset(BCProjectDetailRichListCellTopHeight-25.0);
    }];
    //
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsZero;
    self.richCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(BCProjectDetailCellLeftPadding, 0, ScreenWidth-2*BCProjectDetailCellLeftPadding, 0) collectionViewLayout:layout];
    self.richCollection.dataSource = self;
    self.richCollection.delegate = self;
    self.richCollection.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.richCollection];
    [self.richCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).offset(BCProjectDetailRichListCellTopHeight);
        make.height.mas_equalTo([BCProjectDetailRichCollectionCell defaultHeight]);
        make.width.mas_equalTo(self.contentView).multipliedBy(1.0);
    }];
    //
    [self.richCollection registerClass:BCProjectDetailRichCollectionCell.class forCellWithReuseIdentifier:NSStringFromClass(BCProjectDetailRichCollectionCell.class)];
}

#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.richSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCProjectDetailRichCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BCProjectDetailRichCollectionCell.class) forIndexPath:indexPath];
    cell.cellData = self.richSource[indexPath.row];
    cell.indexPath = indexPath;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    //collectionCount
    NSInteger count = [self collectionView:self.richCollection numberOfItemsInSection:0];
    count = (count>3||count<=0)?3:count;
    CGFloat width = collectionView.frame.size.width/count;
    return CGSizeMake(width, collectionView.frame.size.height);
}





- (void)setCellData:(BCProjectDetailData *)cellData {
    _cellData = cellData;
    //
    self.richLabel.text = cellData.tags.title;
    NSMutableArray *richSource = [NSMutableArray arrayWithCapacity:3];
    NSArray *tags = cellData.tags.data;
    if (tags.count) {
        for (int i=0; i<tags.count; i++) {
            BCProjectDetailDataTagsData *tag = tags[i];
            tag.text = [cellData.tags.text stringByReplacingOccurrencesOfString:@"{}" withString:cellData.data.curr];
            //
            [richSource addObject:tag];
        }
    }
    self.richSource = richSource;
    [self.richCollection reloadData];
    //collectionCount
    NSInteger count = [self collectionView:self.richCollection numberOfItemsInSection:0];
    count = (count>3||count<=0)?3:count;
    CGFloat multiplied = count/3.0;
    if (multiplied==0) {
        multiplied = 1;
    }
    [self.richCollection mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).offset(BCProjectDetailRichListCellTopHeight);
        make.height.mas_equalTo([BCProjectDetailRichCollectionCell defaultHeight]);
        make.width.mas_equalTo(self.contentView).multipliedBy(multiplied);
    }];
}


+ (CGFloat)defaultHeight {
    CGFloat height = 0;
    height += BCProjectDetailRichListCellTopHeight;
    height += [BCProjectDetailRichCollectionCell defaultHeight];
    height += 15.0;
    return height;
}

@end
